from rest_framework import serializers

from goods.models import CateModel, OriginModel, GoodsModel, AssessModel
from user.userser import UserSerializer


class CateSer(serializers.ModelSerializer):
    class Meta:
        model = CateModel
        fields = ['id', 'c_name']


class CateSerializer(serializers.ModelSerializer):
    children = CateSer(many=True)

    class Meta:
        model = CateModel
        fields = '__all__'


class OriginSerializer(serializers.ModelSerializer):
    class Meta:
        model = OriginModel
        fields = '__all__'


class GoodsSerializer(serializers.ModelSerializer):
    g_img = serializers.ImageField(required=False)
    origin_name = serializers.SerializerMethodField(read_only=True)

    def get_origin_name(self, obj):
        return obj.origin.o_name

    class Meta:
        model = GoodsModel
        fields = '__all__'


class AssessSerializer(serializers.ModelSerializer):
    user = UserSerializer()
    goods = GoodsSerializer()

    class Meta:
        model = AssessModel
        fields = '__all__'
